package com.youzi.usercenter.controller;

import com.youzi.corazon.common.exception.ServiceException;
import com.youzi.feign.service.CubeService;
import com.youzi.usercenter.form.LoginForm;
import com.youzi.usercenter.model.TUser;
import com.youzi.usercenter.service.LoginService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by tanxl on 2017-12-18.
 */
@RestController
public class LoginController {

    @Autowired
    LoginService loginService;

    @Autowired
    CubeService cubeService;

    /**
     * 用户登录
     *
     * @param form
     * @return
     */
    @PostMapping("/login")
    public TUser login(@RequestBody LoginForm form) {
        String captcha = cubeService.findCaptchaByToken(form.getToken());
        if (StringUtils.isBlank(captcha) || !captcha.equalsIgnoreCase(form.getCaptcha())) {
            throw new ServiceException("验证码错误");
        }

        return loginService.login(form);

    }


}
